package com.hschinese.pronunciation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.bean.ListViewItem;
import com.hschinese.pronunciation.utils.GridViewUtils;
import com.hschinese.pronunciation.widget.MGridView;

import java.util.ArrayList;
import java.util.List;

public class MsgFlowAdapter extends BaseAdapter {
    private static final int GridViewColumns = 4;

    List<ListViewItem> mItems = new ArrayList<ListViewItem>();
    LayoutInflater mInflater;
    Context mContext;

    public MsgFlowAdapter(Context context, List<ListViewItem> items) {
        mContext = context;
        mItems = items;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public ListViewItem getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.word_listview_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.mGridView = (MGridView) convertView.findViewById(R.id.my_gridview);
            viewHolder.mTextView = (TextView) convertView.findViewById(R.id.my_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final ListViewItem item = getItem(position);
        viewHolder.mGridView.setAdapter(new GridViewAdapter(mContext, item.mWords));
        GridViewUtils.updateGridViewLayoutParams(viewHolder.mGridView, GridViewColumns);
        viewHolder.mTextView.setText(item.mText);
        return convertView;
    }

    static class ViewHolder {
        MGridView mGridView;
        TextView mTextView;
    }

}
